package com.example.testapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

public class Handler4Activity extends ApplicationActivity{
    private Timer timer; // 计时器
    private TextView textView; // 显示倒计时的文本控件
    private int i = 5; // 设置倒数的最大数
    public  static Handler4Activity instance = null; //定义静态的闪屏界面用于打开主界面后关闭该界面
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler4);
        // 全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        instance = this;  // 赋值本界面
        textView = (TextView) findViewById(R.id.text33);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();  // 初始化message
                message.what = 1;
                if(i >= 0){
                    handler.sendMessage(message);  // 发送消息给handler
                }
                if(i == 0){
                    cancel();   // 清除计时器
                }
            }
        },0,1000);  // 每1秒执行一次该方法

    }
    Handler handler = new Handler(new Handler.Callback(){
       @Override
       public boolean handleMessage(Message msg){
           if(msg.what == 1){
               i--; // 秒数倒数-1
               textView.setText("倒计时(" + i + ")");
               if(i == 0){
                   Intent intent = new Intent(Handler4Activity.this,Main2Activity.class);
                   startActivity(intent);
               }
           }
           return false;
       }
    });
}
